﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace AcctrueWMS.Foundation.Data
{
    public class DataModelColumnCollection : InternalDataCollectionBase
    {
        private DataModel datamodel = null;
        public DataModel OwnerModel
        {
            get
            {
                return this.datamodel;
            }
            set
            {
                this.datamodel = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Owner"></param>
        public DataModelColumnCollection(DataModel Owner)
        {
            this.datamodel = Owner;
        }
        /// <summary>
        /// 清空List
        /// </summary>
        public void Clear()
        {
            this.List.Clear();
        }
        /// <summary>
        /// 增加列
        /// </summary>
        /// <param name="column"></param>
        public void Add(DataModelColumn column)
        {
            if (!this.List.Contains(column))
            {
                OwnerModel.ItemArray.Add(column.Name, new DataModelCell()
                {
                    Column = column,
                    Model = OwnerModel,
                    Value = null,
                    ColumnIndex = this.list.Count
                });
                List.Add(column);
            }
            else
            {
                throw new Exception("列表中存在相同的列");
            }
        }
        /// <summary>
        /// 删除列
        /// </summary>
        /// <param name="column"></param>
        public void Remove(DataModelColumn column)
        {
            List.Remove(column);
        }
        /// <summary>
        /// 删除指定的列
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }
        /// <summary>
        /// 属性访问器
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataModelColumn this[string name]
        {
            get
            {
                DataModelColumn column = null;
                foreach (DataModelColumn col in this.List)
                {
                    if (col.Name.ToLower() == name.ToLower())
                    {
                        column = col;
                        break;
                    }
                }
                return column;
                //  return List.Cast<DataModelColumn>().First<DataModelColumn>(item => item.Name.ToLower() == name.ToLower());
            }
        }
        /// <summary>
        /// 属性访问器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DataModelColumn this[int index]
        {
            get
            {
                return List[index] as DataModelColumn;
            }
        }
        public void AddRange(DataModelColumn[] columns)
        {
            this.List.AddRange(columns);
        }
        private ArrayList list;
        protected override System.Collections.ArrayList List
        {
            get
            {
                if (list == null)
                {
                    list = new ArrayList();
                }
                return list;
            }
        }
        public List<DataModelColumn> ToList()
        {
            List<DataModelColumn> list = new List<DataModelColumn>();
            foreach (DataModelColumn column in this.List)
            {
                list.Add(column);
            }
            return list;
        }

        public bool ContainsKey(string key)
        {
            foreach (DataModelColumn col in list)
            {
                if (col.Name == key)
                    return true;
            }
            return false;
        }

    }
}
